let session = require('electron').remote.session
let ses = session.defaultSession

var CookieUtil = {
  // 设置cookie
  set: function (name, value, expires, domain, path, secure) {
    return new Promise((resolve, reject) => {
      let cookieOpt = {
        url: domain, // the url of the cookie.
        name: name, // a name to identify it.
        value: value, // the value that you want to save
        expirationDate: parseInt(expires / 1000)
      }
      console.log('cookieOpt', cookieOpt)
      ses.cookies.set(cookieOpt, function () {
        ses.cookies.flushStore(res => {
          resolve()
        })
        /* console.log(error); */
      })
    })
  },
  // name=value; expires=expiration_time; path=domain_path; domain=domain_name; secure
  // 获取cookie
  get: function (opt) {
    return new Promise(resolve => {
      ses.cookies.get(opt, (error, cookies) => {
        resolve(cookies)
      })
    })
  },
  // 删除cookie
  unset: function (name, domain, path, secure) {
    return this.set(name, '', Date(0), domain, path, secure)
  }
}

export {CookieUtil}
